package com.fourGirlsAndADude.mobileDRT;

import java.util.ArrayList;
import java.util.Hashtable;

import com.att.intern.webservice.Incident;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.TextureView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

/*
 * GLCResultNoActivity
 * This class is for the GLC Search Result with "No" transaction
 */
public class GLCResultNoActivity extends Activity {

	// Incident Array
	//private ArrayList<Incident> ListOfIncidents;

	// String Array of Building Names
	private String[] buildingNames;

	// String Array of Building Info
	private String[] buildingInfo = new String[3];

	// Used to populate a list
	private ArrayAdapter<String> adapter;

	// UI Elements
	private LinearLayout noResultHeaderForm;
	private TextView noResultHeader;
	private TextView noResultSubHeader;
	private ListView noResultListView;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_glcresult_no);

		// Getting List of incidents
		/*ListOfIncidents = (ArrayList<Incident>) this.getIntent()
				.getSerializableExtra("GLC_RESULTS");*/

		// Getting Hashtable
		/*Hashtable<Integer, String> glcInfo = (Hashtable<Integer, String>) this
				.getIntent().getSerializableExtra("HASH_TABLE_GLC_INFO");*/
		
		final int geolocint = this.getIntent().getIntExtra("GEOLOC_INT", 0);
		final String user = this.getIntent().getStringExtra("USERNAME");
		final String session = this.getIntent().getStringExtra("SessionToken");
		final String authorizationType = this.getIntent().getStringExtra("AUTHORIZATIONTYPE");

		//Log.d("NoResult", "Size of ListOfIncidents : " + ListOfIncidents.size());
		/*
		 * for(Incident incident: ListOfIncidents){ Log.d("NoResult",
		 * "Incident building name: " + incident.getBuildingName()); }
		 */

		Log.d("NoResult", "Displaying UI Elements");

		// Intialize UI elements
		noResultHeader = (TextView) findViewById(R.id.noResultHeader);
		noResultSubHeader = (TextView) findViewById(R.id.noResultSubHeader);

		noResultHeaderForm = (LinearLayout) findViewById(R.id.noResultHeaderForm);

		// Filling array with building names
		Log.d("NoResult ", "Intializng Building names");
		getInfoFromHash(geolocint);
		
		
		//Getting info from 
		//intializeBuildingNames(ListOfIncidents);
		/*
		 * buildingNames = new String[ListOfIncidents.size()]; for (int i = 0; i
		 * < ListOfIncidents.size(); i++) { buildingNames[i] =
		 * (ListOfIncidents.get(i).getBuildingName()); }
		 */

		Log.d("NoResult ", "Creating array adapter");
		// Intializing array adapter with builgin names
		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, buildingNames);

		// Intializing List View
		Log.d("NoResult ", "Creating list view");
		noResultListView = (ListView) findViewById(R.id.noResultListView);

		// Adding Adapter with building names
		Log.d("NoResult ", "setting adapter to list view");
		noResultListView.setAdapter(adapter);

		Log.d("NoResult ", "creating on item click listener");
		noResultListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Log.d("NoResult ", "Intializing dialog");
				AlertDialog.Builder dialog = new AlertDialog.Builder(
						GLCResultNoActivity.this);
				// display Title
				Log.d("NoResult ", "displaying title");
				dialog.setTitle(buildingNames[arg2])
						.setItems(buildingInfo,
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										// TODO Auto-generated method stub

									}// End of onClick for
										// DialogInterface.OnClickListener
								})// End of onClickListener
						.setNegativeButton(R.string.Exit,
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										// TODO Auto-generated method stub
										dialog.cancel();
									}// End of onClick
								})// End of OnClickListener for negative button
						.setPositiveButton(R.string.add,
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										// TODO Auto-generated method stub
										String state = buildingInfo[1];
										String pm = buildingInfo[2];
										String buildingName = buildingNames[0];
										String address = buildingInfo[0];
										Intent i = new Intent(getBaseContext(),
												AddAssesmentActivity.class);
										i.putExtra("GLC", geolocint);
										i.putExtra("STATE", state);
										i.putExtra("PM", pm);
										i.putExtra("BUILDINGNAME", buildingName);
										i.putExtra("ADDRESS", address);
										i.putExtra("USER", user);
										i.putExtra("SESSION", session);
										i.putExtra("AUTHTYPE", authorizationType);
										startActivity(i);
										dialog.cancel();

									}// End Of onClick
								});// End Of onClickListener
				Log.d("NoResult ", "Created title, list,and buttons");
				// Display Dialog Box
				Log.d("NoResult ", "Creating box");
				AlertDialog displayInfo = dialog.create();
				displayInfo.show();
			}// End Of onItemClick
		});// End Of setOnItemClickListener
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.glcresult_no, menu);
		return true;
	}

	/*public void intializeBuildingInfo(Incident incident) {
		Log.d("NoResult ", "Intializing buidling info Array");
		// Used to hold values for Array
		String glc;
		String state;
		String pm;

		// Assign building info to display to strings
		glc = Integer.toString(incident.getGeoLoc());
		state = incident.getState();
		pm = incident.getPMAttuid();

		// Populate Array with building info
		buildingInfo[0] = glc;
		buildingInfo[1] = state;
		buildingInfo[2] = pm;
		Log.d("NoResult ", "Array Intialized");
	}

	public void intializeBuildingNames(ArrayList<Incident> list) {
		Log.d("NoResult ", "Intializing building names");
		Log.d("NoResult ", "Size of array list" + list.size());
		buildingNames = new String[list.size()];
		Log.d("NoResult ", "Size of array" + buildingNames.length);
		for (int i = 0; i < list.size(); i++) {
			buildingNames[i] = list.get(i).getBuildingName();

		}

		for (String name : buildingNames) {
			Log.d("NoResult", "Building Name: " + name);
		}
		Log.d("NoResult ", "Array Intialized");
	}*/

	@SuppressLint("UseValueOf")
	public void getInfoFromHash(int geolocint) {
		Log.v("NoResult", "Get hashtable form webService");
		Hashtable<Integer,String> glcInfo = LoginActivity.webService.getGLCInfo();
		
		Log.d("NoResult","Getting String from Hash table");
		String glcvalue = glcInfo.get( Integer.valueOf(geolocint));
		
		//Splitting string which has been delimenated by "~"
		Log.d("NoResult", "Splitting String");
		String[] glcvalues = glcvalue.split("~");
		
		Log.v("NoReulst", "Assign string values");
		String state = glcvalues[0];
		String pm = glcvalues[1];
		String buildingName = glcvalues[2];
		String address = glcvalues[3];
		
		//Fill buildingNames
		Log.v("NoResult", "Filling buildings names array");
		buildingNames = new String [1];
		buildingNames[0] = buildingName;
		
		//Fill buildingInfo
		Log.v("No Result", "Filling building info array");
		buildingInfo[0] = "Address: "+address;
		buildingInfo[1] = "State: "+state;
		buildingInfo[2] = "PM: "+pm;
		
		Log.v("NoResult", "End of getInfoFromHash");
	}

}// end class
